package com.headwind.logistics.permission.controller;

import com.headwind.logistics.common.util.*;
import com.headwind.logistics.log.annotate.Log;
import com.headwind.logistics.permission.dto.EmpRoleDTO;
import com.headwind.logistics.common.pojo.Employee;
import com.headwind.logistics.common.pojo.Role;
import com.headwind.logistics.permission.service.intl.EmployeeServiceIntl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 小恶魔
 * 员工控制器层
 *
 */
@Controller
public class EmployeeController {

    //员工业务逻辑层
    @Resource
    private EmployeeServiceIntl employeeServiceIntl;

    //api统一响应格式
    @Resource
    private ResultGenerator resultGenerator;

    /**
     * 后台临时登录获取所有员工
     * @return
     */
    @GetMapping("/admin/tempQueryEmp")
    @ResponseBody
    public RestResult queryAllEmpTempLogin(){

        return resultGenerator.getSuccessResult(employeeServiceIntl.tempLogin());
    }

    /**
     * 获取员工与角色 表格
     * @return
     */
    @GetMapping("/admin/queryEmployeeRolePage")
    @ResponseBody
    @Log("查看所有的员工")
    public LayuiUtil queryEmployeeRole(Integer page,Integer limit){

        List<EmpRoleDTO> empRoleDTOS = employeeServiceIntl.queryAllEmpRolePage(page, limit);


        return LayuiUtil.data(employeeServiceIntl.queryAllEmpRolePage(null, null).size(),empRoleDTOS);
    }

    /**
     * 查询全部角色
     * @return
     */
    @PostMapping("/admin/queryAllRole")
    @ResponseBody
    public Map<String,Object> queryAllRole(){

        Map<String,Object> map = new HashMap<>();
        List<Role> roles = employeeServiceIntl.queryAllRole();

        if (roles != null){
            map.put("code",200);
            map.put("data",roles);
        }else{
            map.put("code",400);
            map.put("data",null);
        }

        return map;
    }

    /**
     * 添加员工
     * @param employee
     * @param departmentId
     * @param roleId
     * @return
     */
    @PostMapping("/admin/addEmployee")
    @ResponseBody
    @Log("添加了员工")
    public Map<String,Object> addEmployee(Employee employee,Long departmentId,String roleId){
        Map<String,Object> map = new HashMap<>();

        try {

            employee.setDepartmentId(departmentId);

            int count = employeeServiceIntl.addEmployee(employee, roleId);

            if (count>0){
                map.put("code",200);
                return map;
            }

            map.put("code",400);

        }catch (Exception e){
            throw e;
        }

        return map;
    }

    /**
     * 根据员工id修改员工
     * @param employee
     * @return
     */
    @PostMapping("/admin/updateEmployeeByEmpId")
    @ResponseBody
    public RestResult updateEmployeeByEmpId(Employee employee,HttpSession session){

        int count = 0;
        try {
            //修改用户
            count = employeeServiceIntl.updateEmployeeByEmpId(employee,session);

            if (count >0){

                return resultGenerator.getSuccessResult("更改成功",count);

            }

        }catch(Exception e){

            return resultGenerator.getFreeResult(ResultCode.INTERNAL_SERVER_ERROR,"服务器错误",0);

        }

        return resultGenerator.getFailResult("更改失败",count);

    }

    /**
     * 根据姓名、性别、职位、入司时间、员工号 搜索员工
     * @param empRoleDTO
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/admin/queryEmployeeByNameOrRole")
    @ResponseBody
    public LayuiUtil queryEmployeeByNameOrSexOrRoleOrTimeOrNo(EmpRoleDTO empRoleDTO,Integer page,Integer limit){

        List<EmpRoleDTO> empRoleDTOS = employeeServiceIntl.queryEmployeeByNameOrSexOrRoleOrTimeOrNo(empRoleDTO, page, limit);

        return LayuiUtil.data(employeeServiceIntl.queryEmployeeByNameOrSexOrRoleOrTimeOrNo(empRoleDTO, null, null).size()
                ,empRoleDTOS);
    }

    /**
     * 验证员工密码是否正确
     * @param password
     * @param session
     * @return
     */
    @PostMapping("/admin/validateEmpPassword")
    @ResponseBody
    public RestResult validateEmpPassword(String password,HttpSession session){

        //从session中获取员工对象
        Employee employee = (Employee) session.getAttribute("EMPLOYEE");

        //传进来的密码进行md5加密
        String md5Pass = Md5Util.getMD5(password);

        if (employee.getPassword().equals(md5Pass)){
            return resultGenerator.getSuccessResult();
        }

        return resultGenerator.getFailResult("验证错误");
    }

    /**
     * 根据员工id获取角色
     * @param empId
     * @return
     */
    @GetMapping("/admin/queryRoleByEmpId/{empId}")
    @ResponseBody
    public RestResult queryRoleByEmpId(@PathVariable("empId")Long empId){

        return resultGenerator.getSuccessResult("获取成功",employeeServiceIntl.queryRoleByEmpId(empId));
    }

    /**
     * 添加或删除角色员工表
     * @param employeeId
     * @param roleId
     * @param type
     * @return
     */
    @PostMapping("/admin/addOrDeleteEmpRole")
    @ResponseBody
    public RestResult addOrDeleteEmpRole(Long employeeId,Long roleId,boolean type){

        Integer count = employeeServiceIntl.addOrDeleteEmpRole(roleId, employeeId, type);

        if (count >0){
            return resultGenerator.getSuccessResult();
        }

        return resultGenerator.getFailResult("更改失败了呢");
    }

}
